package com.ypx.ec.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ypx.ec.product.entity.AttrAttrgroupRelationEntity;
import com.ypx.ec.product.entity.AttrEntity;
import com.ypx.ec.product.service.AttrAttrgroupRelationService;
import com.ypx.ec.product.service.AttrService;
import com.ypx.ec.product.service.CategoryService;
import com.ypx.ec.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ypx.ec.product.entity.AttrGroupEntity;
import com.ypx.ec.product.service.AttrGroupService;
import com.ypx.ec.common.utils.PageUtils;
import com.ypx.ec.common.utils.R;



/**
 * 属性分组
 *
 * @author yepinxi
 * @email 1554039596@qq.com
 * @date 2022-03-10 16:57:27
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 用当前当前分类id查询完整路径并写入 attrGroup
        attrGroup.setCatelogPath(categoryService.findCateLogPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     *根据分组id查出所有关联的属性
     * @param
     * @return
     */
    @GetMapping("{attrGroupId}/attr/relation")
    public R attrGroupRelation(@PathVariable Long attrGroupId){
        List<AttrEntity> list=attrService.getAttrByGroupId(attrGroupId);
        return R.ok().put("data",list);
    }
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R listNoRelation(@RequestParam Map<String, Object> params,@PathVariable Long attrGroupId){
       PageUtils page= attrService.getNoAttrByAttrtype(params,attrGroupId);
       return R.ok().put("page",page);
    }
    @GetMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupVo> data=attrGroupService.queryAllGroupAndAttr(catelogId);
        return R.ok().put("data",data);
    }

    /**
     * 添加属性关联
     * @param
     * @return
     */
    @PostMapping("attr/relation")
    public R addAttrRelation(@RequestBody AttrAttrgroupRelationEntity[] entity){
        attrAttrgroupRelationService.saveBatch(Arrays.asList(entity));
        return R.ok();
    }

    /**
     *
     * @param entity
     * @return
     */
    @PostMapping("attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrAttrgroupRelationEntity[] entity){
        attrAttrgroupRelationService.removeBatch(Arrays.asList(entity));
        return R.ok();
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }




}
